package utils.log
{
	
	import flash.events.TimerEvent;
	import flash.filesystem.File;
	import flash.net.getClassByAlias;
	import flash.system.System;
	import flash.utils.Timer;
	
	import mx.formatters.DateFormatter;
	import mx.logging.Log;
	import mx.logging.LogEvent;
	
	import utils.filemanager.MyFileWriter;

	public class LOGGER
	{
		private static const TYPE_INTERACTION:String = "interaction";
		private static const TYPE_TIMEEVENT:String 	= "timeEvent";
		
		public static const CONTENT_INTERACT_NEXTCLICK:String 			= "nextAdvertClick";
		public static const CONTENT_INTERACT_PREVCLICK:String 			= "prevAdvertClick";
		public static const CONTENT_INTERACT_ONIMGCLICK:String 			= "advImgClick";
		public static const CONTENT_INTERACT_BACKCLICK:String 			= "backClick";
		public static const CONTENT_INTERACT_HELPOPEN:String 			= "helpOpen";
		public static const CONTENT_INTERACT_HELPCLOSE:String 			= "helpClose";
		public static const CONTENT_INTERACT_VIDEOINTER:String 			= "interactVideo";
		public static const CONTENT_INTERACT_VIDEOPLAYING:String		= "playVideo";
		public static const CONTENT_INTERACT_VIDEOFINISHED:String		= "videoFinished";
		
		
		public static const CONTENT_TIME_SUBTIMEOUT:String				= "subAdv_timeout";
		public static const CONTENT_TIME_CANCELSUBTIMEOUT:String		= "videoCancelTimeOut";
		
		
		private static const NL:String =	"\r\n";
		public static function log(whatToLog : String) : String
		{
			var returnString : String = "["+currentDateTimeString()+"] \""+whatToLog+"\"";			
			var file:File;
			
			if (!File.applicationStorageDirectory.resolvePath("log/"+currentDateString()+".log").exists){
				MyFileWriter.write(returnString+NL,File.applicationStorageDirectory.nativePath+"/log/",currentDateString()+".log");
			}else{
				MyFileWriter.append(returnString+NL,File.applicationStorageDirectory.nativePath+"/log/"+currentDateString()+".log");
			}
			
			trace(returnString);
			return returnString + "\n";
		}
		

		
		
		private static function getAdvLogg(type:String=""):XML{
			if (File.applicationStorageDirectory.resolvePath("log/advertising/log_"+currentDateString()+".xml").exists)
				return new XML(MyFileWriter.read(File.applicationStorageDirectory.nativePath.toString()+"/log/advertising/log_"+currentDateString()+".xml"));
			else{
				LOGGER.log("no "+type+".log found, generating....");
				generateNewAdvLogFile(type);
				return new XML(MyFileWriter.read(File.applicationStorageDirectory.nativePath.toString()+"/log/advertising/log_"+currentDateString()+".xml"));
			}
		}
		
		private static function writeToAdvLogg(newContent:XML):void{
			MyFileWriter.write(newContent,File.applicationStorageDirectory.nativePath+"/log/advertising/","log_"+currentDateString()+".xml")
		}
		
		private static function generateNewAdvLogFile(type:String):void{
			var log:XML = <log/>;
			log.help = "help";
			MyFileWriter.write(log,File.applicationStorageDirectory.nativePath+"/log/advertising/","log_"+currentDateString()+".xml");
		}
		
		
		
		
		public static function advertLog(content:String ,pageID:String ):void{
			var TYPE:String = getTypeFromContent(content);
			var logContent:XML;
			logContent = getAdvLogg();
			var date:String = currentDateString("YYYY-MM-DD");
			var time:String = currentTimeString("HH:NN:SS");
			var newElement:XML;
			switch(TYPE)
			{
				case TYPE_INTERACTION:
				{
					newElement = <interaction/>;
					newElement.@date = date;
					newElement.@time = time;
					newElement.@pageID = pageID;
					newElement.@type	= content;
					logContent.appendChild(newElement);
					break;
				}
				case TYPE_TIMEEVENT:
				{
					newElement = <timeEvent/>;
					newElement.@date = date;
					newElement.@time = time;
					newElement.@pageID = pageID;
					newElement.@type	= content;
					logContent.appendChild(newElement);
				}
				
			}
			// wenn neu erzeugt wurde, help element wieder löschen
			delete logContent.help;

			// in Datei schreiben
			writeToAdvLogg(logContent);
	}
		
		private static function getTypeFromContent(content:String):String{
			var timeArray:Array			= ["subAdv_timeout","videoCancelTimeOut"]
			if(timeArray.indexOf(content)>=0)
				return TYPE_TIMEEVENT;
			else			
				return TYPE_INTERACTION;
			
		}
		
		
		private static function currentDateTimeString():String
		{               
			var CurrentDateTime:Date = new Date();
			var CurrentDF:DateFormatter = new DateFormatter();
			CurrentDF.formatString = "DD/MMM/YYYY:HH:NN:SS:QQQ"
			var DateTimeString:String = CurrentDF.format(CurrentDateTime);
			return DateTimeString;
		}
		private static function currentDateString(formatString:String = "DD-MMM-YYYY"):String
		{               
			var currentDateTime:Date = new Date();
			var currentDF:DateFormatter = new DateFormatter();
			currentDF.formatString = formatString;
			var dateTimeString:String = currentDF.format(currentDateTime);
			return dateTimeString;
		}
		private static function currentTimeString(formatString:String):String{
			var currentTime:Date = new Date();
			var currentDF:DateFormatter = new DateFormatter();
			currentDF.formatString = formatString;
			return currentDF.format(currentTime);
		}
		
		private static function doubleDigitFormat(num:uint):String 
		{
			if(num < 10) {
				return ("0" + num);
			}
			return num.toString();
		}
	
	}
	
}